﻿using System;
using System.IO.Ports;
using System.Text;
using NetduinoRobot.Common;

namespace ControlCenter.Communication
{
    /// <summary>
    /// SerialPortWrapper implementation
    /// Written by: Salvador Richter
    /// Appache License Version 2.0 
    /// </summary>
    public class SerialPortWrapper : ISerialPortWrapper
    {
        private SerialPort _serialPort = null;

        public SerialPortWrapper(string portName, int portSpeed)
        {
            _serialPort = new SerialPort(portName, portSpeed);
        }

        public bool IsOpen()
        {
            return _serialPort.IsOpen;
        }

        public void Open()
        {
            _serialPort.Open();
        }

        public void Close()
        {
            _serialPort.Close();
        }

        public int BytesToRead()
        {
            return _serialPort.BytesToRead;
        }

        public int Read(byte[] cmdBuffer, int cmdBufferOffset, int readLength)
        {
            return _serialPort.Read(cmdBuffer, cmdBufferOffset, readLength);
        }

        public void Write(string s)
        {
            _serialPort.Write(UTF8Encoding.UTF8.GetBytes(s), 0, s.Length);
        }
    }
}
